<?php

class PutRelationshipAction extends AdminAction
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        //分页每页的条数
        $perPage = $this->getPerPage();

        //查询条件
        $where = array();

        //登录用户信息
        $user = Session('admin');
        $user_id = $user['user_id'];
        $where['advertising_user_id'] = $user_id;

        $currPage = intval($_GET['p']);
        $currPage < 1 && $currPage = 1;

        $ad_putRelationship = M("putrelationship");

        $itemCount = $ad_putRelationship->where($where)->count();
        $data = $ad_putRelationship->where($where)->order('rel_id desc')->limit(($currPage-1)*$perPage, $perPage)->select();

        $itemPerPage = $perPage;
        $pageUrl = $this->baseUrl . '/Admin/PutRelationship/index';
        $paramQuery = array();
        $paginator = new Paginator($currPage, $itemCount, $itemPerPage, $pageUrl, $paramQuery);

        $page = $paginator->getHtml(1);

        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('data', $data);
        $this->assign('page', $page);

        $this->display();

    }

    /**
     * 增加投放信息
     */
    public function add()
    {
        //登录用户信息
        $user = Session('admin');
        $user_id = $user['user_id'];
        $where['user_id'] = $user_id;

        //获取登录用户下的所有广告
        $ad = M('advertising');
        $advertisingInfo = $ad->where($where)->field('advertising_id, advertising_name')->select();

        //获取所有网站
        $site = M('site');
        $siteInfo = $site->field('site_id, site_name')->select();

        $this->assign('advertisingInfo', $advertisingInfo);
        $this->assign('siteInfo', $siteInfo);
        $this->assign('baseUrl', $this->baseUrl);
        $this->display();

    }

    public function addOperate()
    {
        //广告ID
        $advertising_id = intval($_POST['advertising_id']);

        //网站ID
        $site_id = intval($_POST['site_id']);

        $ad_putRelationship = M("putrelationship");

        //同一广告只能在同一站点投放一次
        $count = $ad_putRelationship->where(array('advertising_id' => $advertising_id, 'site_id' => $site_id))->count();
        if ($count) {
            exit('投放错误，同一广告只能在同一站点投放一次');
        }

        //获取广告信息
        $ad = M('advertising');
        $advertisingInfo = $ad->where(array('advertising_id'=>$advertising_id))
            ->field('advertising_name, user_id')
            ->find();

        //获取网站信息
        $site = M('site');
        $siteInfo = $site->where(array('site_id'=>$site_id))
            ->field('site_name, user_id, rate')
            ->find();

        //组装数据
        $data['advertising_id']      = $advertising_id;
        $data['advertising_name']    = $advertisingInfo['advertising_name'];
        $data['advertising_user_id'] = $advertisingInfo['user_id'];
        $data['site_id']             = $site_id;
        $data['site_name']           = $siteInfo['site_name'];
        $data['site_user_id']        = $siteInfo['user_id'];
        $data['source']              = encrypt_str($advertising_id . "\t" . $site_id);
        $data['site_code']           = md5(time() . $site_id);
        $data['advertising_code']    = md5(time() . $advertising_id);
        $data['platform_and_site']   = $_POST['platform_and_site'];
        $data['site_and_user']       = $_POST['site_and_user'];
        $data['rate']                = !empty($_POST['rate']) ? $_POST['rate'] : $siteInfo['rate'];
        $data['data_back']           = $_POST['data_back'];
        $data['begin_time']          = strtotime($_POST['begin_time']);
        $data['is_offline']          = 0;
        $data['create_time']         = time();
        $data['update_time']         = 0;

        $re = $ad_putRelationship->add($data);
        if ($re) {
            alert('success','投放成功', $this->baseUrl . "/Admin/PutRelationship/index");
        } else {
            alert('error', '投放失败，请重新再试');
        }

    }

    public function edit()
    {
        //登录用户信息
        $user = Session('admin');
        $user_id = $user['user_id'];

        $rel_id = intval($_GET['rel_id']);

        $ad_putRelationship = M("putrelationship");

        $putRelationshipinfo = $ad_putRelationship->where(array('rel_id' => $rel_id))->find();

        if (empty($putRelationshipinfo)) {
            exit('修改错误，没有这条投放信息');
        }elseif ($putRelationshipinfo['advertising_user_id'] != $user_id) {
            exit('没有权限修改');
        }elseif ($putRelationshipinfo['begin_time'] <= time()) {
            exit('该广告已经开始不能修改');
        }

        $where['user_id'] = $user_id;

        //获取登录用户下的所有广告
        $ad = M('advertising');
        $advertisingInfo = $ad->where($where)->field('advertising_id, advertising_name')->select();

        //获取所有网站
        $site = M('site');
        $siteInfo = $site->field('site_id, site_name')->select();

        $this->assign('advertisingInfo', $advertisingInfo);
        $this->assign('siteInfo', $siteInfo);
        $this->assign('putRelationshipinfo', $putRelationshipinfo);
        $this->assign('baseUrl', $this->baseUrl);
        $this->display();

    }

    public function editOperate()
    {
        $rel_id = intval($_POST['rel_id']);

        $ad_putRelationship = M("putrelationship");
        $putRelationship = $ad_putRelationship->where(array('rel_id'=>$rel_id))->find();

        //广告ID
        //$advertising_id = intval($_POST['advertising_id']);

        //网站ID
        //$site_id = intval($_POST['site_id']);

        //获取广告信息
        //$ad = M('advertising');
//        $advertisingInfo = $ad->where(array('advertising_id'=>$advertising_id))
//            ->field('advertising_name, user_id')
//            ->find();

        //获取网站信息
//        $site = M('site');
//        $siteInfo = $site->where(array('site_id'=>$site_id))
//            ->field('site_name, user_id, rate')
//            ->find();

        //组装数据
        $data['rel_id']              = intval($_POST['rel_id']);
        //$data['advertising_id']      = $advertising_id;
        //$data['advertising_name']    = $advertisingInfo['advertising_name'];
        //$data['advertising_user_id'] = $advertisingInfo['user_id'];
        //$data['site_id']             = $site_id;
        //$data['site_name']           = $siteInfo['site_name'];
        //$data['site_user_id']        = $siteInfo['user_id'];
        $data['platform_and_site']   = $_POST['platform_and_site'];
        $data['site_and_user']       = $_POST['site_and_user'];
        $data['rate']                = !empty($_POST['rate']) ? $_POST['rate'] : $siteInfo['rate'];
        $data['data_back']           = $_POST['data_back'];
        $data['begin_time']          = strtotime($_POST['begin_time']);
        $data['update_time']         = time();


        $re = $ad_putRelationship->save($data);
        if ($re) {
            alert('success','修改成功', $this->baseUrl . "/Admin/PutRelationship/index");
        } else {
            alert('error', '修改失败，请重新再试');
        }

    }

    public function delete()
    {
        $rel_id = intval($_GET['rel_id']);

        $ad_putRelationship = M("putrelationship");

        $putRelationshipinfo = $ad_putRelationship->where(array('rel_id' => $rel_id))->find();

        if (empty($putRelationshipinfo)) {
            exit('投放关系不存在，删除错误');
        }

        $re = $ad_putRelationship->where(array('rel_id' => $rel_id))->delete();
        if ($re) {
            alert('success','删除成功', $this->baseUrl . "/Admin/PutRelationship/index");
        } else {
            alert('error', '删除失败，请重新再试');
        }
    }

    public function detail()
    {
        $rel_id = intval($_GET['rel_id']);

        $ad_putRelationship = M("putrelationship");

        $putRelationshipinfo = $ad_putRelationship->where(array('rel_id' => $rel_id))->find();

        $this->assign('putRelationshipinfo', $putRelationshipinfo);
        $this->assign('http_referer', $_SERVER['HTTP_REFERER']);
        $this->display();
    }

}


?>